package com.mac.intellect_flow.flowable.listener;

import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.HistoryService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.stereotype.Component;

/**
 * 全局监听器
 * @author: mac
 */
@Slf4j
@Component
public class GlobalEventListener extends AbstractFlowableEngineEventListener {

    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {
        HistoryService historyService = SpringUtil.getBean(HistoryService.class);
        String processInstanceId = event.getProcessInstanceId();
        String processDefinitionId = event.getProcessDefinitionId();
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(processInstanceId).singleResult();
        log.info("流程实例ID:{},流程定义ID:{},流程定义名称:{},流程定义版本:{},流程定义key:{},流程定义启动时间:{},流程定义发起人:{},",
                processInstanceId, processDefinitionId, historicProcessInstance.getProcessDefinitionName(),historicProcessInstance.getProcessDefinitionVersion(), historicProcessInstance.getProcessDefinitionKey(),
                historicProcessInstance.getStartTime(), historicProcessInstance.getStartUserId());
    }
}
